
//1.导入框架 模板
const express = require("express");
const path = require('path');
const open = require('open');
const morgan = require("morgan"); 
const moment = require('moment')
const errormidd=require("./errormiddleware")
const notFound = require('./notFoundmiddleware');
const artTemplate = require('art-template')
const express_temmplate = require('express-art-template')
const app = express();
const port = 5700;

// 配置模板引擎
app.set('views',__dirname+'/Learningnetwork/');// 配置模板的路径
app.engine('html',express_temmplate);// 设置模板后缀为.html的文件(不设这句话，模板文件的后缀默认是.art)
app.set('view engine', 'html');// 设置视图引擎为上面的html

//定义过滤器（函数）
artTemplate.defaults.imports.dateFormat = function(time,format = 'YYYY-MM-DD HH:mm:ss'){
    return moment.unix(time).format(format)
}
// 请求日志中间件
// app.use(request_log)

app.get('/',(req,res)=>{
    let head = ['首页','课程','职业规划']
    let frontEnd = ['前端开发','前端开发','前端开发','前端开发','前端开发','前端开发','前端开发','前端开发']
    let curriculum = ['我的课程表']
    let LanguageDesign = [
        {name:'继续学习 程序语言设计',names:'正在学习-使用对象'},
        {name:'继续学习 程序语言设计',names:'正在学习-使用对象'},
        {name:'继续学习 程序语言设计',names:'正在学习-使用对象'}
    ]

    let Timetable = ['精品推荐','JQuery','Spark','Mysql','JavaWeb','Javascript','Vue','修改兴趣']
    let recommend = [
        {name:"fl",names:'精品推荐1111'},
        {name:"fr",names:'查看~全部'}
    ]
    let list = [
        
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        }
    ]
    let lists = [
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
        {
            attribute:"photoWrap",attributes:"photo",
            photo:"/image/lesson.png",text:"text",title:"title text-ellipsisl2",
            level:"level",study:"study",icon:"icon",image:"/image/hot.png"
        },
    ]
    let VideoLlist = ['HTML从入门到入土 CSS还有酷炫的动画CSS还有酷炫的动画CSS还有酷炫的动画']
    let RecommendationForm = ['高级','1125人在学生']
    let all = {
        
    }
    res.render('index.html',{
        head,frontEnd,LanguageDesign,
        list,lists,Timetable,recommend,VideoLlist,RecommendationForm,
    })
})



app.use(function(req,res,next){
    console.log('middleware1')
    next();
})

app.use(function(req,res,next){
    console.log('middleware2')
    console.log(def);// 触发错误中间件
    next()
})
app.use(errormidd)

app.use(morgan(':method :url :status :res[content-length] - :response-time ms'))

// app.get('/',(req,res)=>{
//     res.sendFile( path.join(__dirname,'Learningnetwork/index.html') )
// })

//托管静态资源
app.use(express.static('Learningnetwork'));

app.get('/index',(req,res)=>{
    // 读取文件内容并响应给浏览器
    res.sendFile(path.join(__dirname,'./Learningnetwork./index.html'))
})

//404页面
 app.use(notFound)


app.listen(port,()=>{
    //自动打开open
    open(`http://127.0.0.1:${port}`)
    console.log(`server is running at port ${port}`)
})